package com.lishe.lishe.Imageloader;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.DecodeFormat;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.RequestOptions;
import com.bumptech.glide.request.target.SimpleTarget;
import com.bumptech.glide.request.transition.Transition;
import com.bumptech.glide.util.Util;
import com.lishe.lishe.R;
import com.lishe.lishe.lishe.utils.Utils;

/**
 * Created lwa on 2017/8/15.
 */

public class ImageLoaderUtils {

    public static void displayImage(final String url, final ImageView imageView, int defaultImg) {
        if (Util.isOnMainThread() && imageView.getContext() != null) {
            Glide.with(imageView.getContext()).load(url).apply(getRequestOption(defaultImg)).into(imageView);
        }
    }

    public static void displayImage(Context activity, final String url, final ImageView imageView, int defaultImg) {
        try {
            if (Util.isOnMainThread() && activity != null) {
                Glide.with(activity).load(url).apply(getRequestOption(defaultImg)).into(imageView);
            }
        } catch (Exception e) {
            e.printStackTrace();
            imageView.setImageResource(defaultImg);
        }
    }

    public static void displayImage(Activity activity, final String url, final ImageView imageView, int defaultImg) {
        try {
            if (Util.isOnMainThread() && activity != null && !activity.isFinishing()) {
                Glide.with(activity).load(url).apply(getRequestOption(defaultImg)).into(imageView);
            }
        } catch (Exception e) {
            e.printStackTrace();
            imageView.setImageResource(defaultImg);
        }
    }

    public static void displayImage(Activity activity, final String url, final ImageView imageView) {
        displayImage(activity, url, imageView, R.drawable.defalut_img);
    }


    public static void displayImage(Fragment fragment, final String url, final ImageView imageView, int defaultImg) {
        try {
            if (Util.isOnMainThread() && fragment != null && !fragment.isDetached()) {
                Glide.with(fragment).load(url).apply(getRequestOption(defaultImg)).into(imageView);
            }
        } catch (Exception e) {
            e.printStackTrace();
            imageView.setImageResource(defaultImg);
        }
    }
    public static void displayImage(Fragment fragment, final String url, final ImageView imageView) {
        try {
            if (Util.isOnMainThread() && fragment != null && !fragment.isDetached()) {
                Glide.with(fragment).load(url).apply(getRequestOption(R.drawable.defalut_img)).into(imageView);
            }
        } catch (Exception e) {
            e.printStackTrace();
            imageView.setImageResource(R.drawable.defalut_img);
        }
    }

    public static void displayCircleImage(Activity activity, final String url, final ImageView imageView, int defaultImg) {
        try {
            if (Util.isOnMainThread() && activity != null && !activity.isFinishing()) {
                Glide.with(activity).load(url).apply(getCircleRequestOption(defaultImg)).into(imageView);
            }
        } catch (Exception e) {
            e.printStackTrace();
            imageView.setImageResource(defaultImg);
        }
    }

    public static void displayCircleImage(Fragment fragment, final String url, final ImageView imageView, int defaultImg) {
        try {
            if (Util.isOnMainThread() && fragment != null && !fragment.isDetached()) {
                Glide.with(fragment).load(url).apply(getCircleRequestOption(defaultImg)).into(imageView);
            }
        } catch (Exception e) {
            e.printStackTrace();
            imageView.setImageResource(defaultImg);
        }
    }

    public static void displayRoundImage(Fragment fragment, final String url, final ImageView imageView, int defaultImg) {
        try {
            if (Util.isOnMainThread() && fragment != null && !fragment.isDetached()) {
                Glide.with(fragment).load(url).apply(getRoundRequestOption(5, defaultImg)).into(imageView);
            }
        } catch (Exception e) {
            e.printStackTrace();
            imageView.setImageResource(defaultImg);
        }
    }

    public static void displayRoundImage(Activity activity, final String url, final ImageView imageView, int defaultImg) {
        try {
            if (Util.isOnMainThread() && activity != null && !activity.isFinishing()) {
                Glide.with(activity).load(url).apply(getRoundRequestOption(5, defaultImg)).into(imageView);
            }
        } catch (Exception e) {
            e.printStackTrace();
            imageView.setImageResource(defaultImg);
        }
    }

    public static void displayTopRoundImage(Activity activity, final String url, final ImageView imageView, int defaultImg) {
        try {
            if (Util.isOnMainThread() && activity != null && !activity.isFinishing()) {
                Glide.with(activity).load(url).apply(getRoundRequestOption(Utils.dip2px(activity, 5),
                        GlideRoundTransform.CORNER_TOP_LEFT_RIGHT, defaultImg)).into(imageView);
            }
        } catch (Exception e) {
            e.printStackTrace();
            imageView.setImageResource(defaultImg);
        }
    }

    /**
     * 载图本地
     *
     * @param context
     * @param url
     * @param imageView
     */
    public void displayLocationImage(Activity context, final String url, final ImageView imageView) {
        try {
            if (Util.isOnMainThread() && context != null && !context.isFinishing()) {
                Glide.with(context).load(url).apply(getRequestOption()).into(imageView);
            }
        } catch (Exception e) {
            e.printStackTrace();
            imageView.setImageResource(R.drawable.defalut_img);
        }
    }

    /**
     * 载图本地
     *
     * @param url
     * @param imageView
     */
    public static void displayLocationImages(Activity activity, final String url, final ImageView imageView) {
        try {
            if (Util.isOnMainThread() && imageView.getContext() != null && !activity.isFinishing()) {
                Glide.with(activity).load("file://" + url).apply(getRequestOption(R.drawable.defalut_img)).into(imageView);
            }
        } catch (Exception e) {
            e.printStackTrace();
            imageView.setImageResource(R.drawable.defalut_img);
        }
    }

    public void setImageBitmap(Activity context, final String url, final ImageView imageView) {
        Glide.with(context).load(url).apply(getRequestOption()).into(new SimpleTarget<Drawable>() {
            @Override
            public void onResourceReady(@NonNull Drawable resource, @Nullable Transition<? super Drawable> transition) {
                imageView.setImageDrawable(resource);
            }
        });
    }

    public static void setImageResource(Activity activity, final String url, final ImageView imageView, int defaultImageId) {
        Glide.with(activity).load(url).apply(getRequestOption(defaultImageId)).into(new SimpleTarget<Drawable>() {
            @Override
            public void onResourceReady(@NonNull Drawable resource, @Nullable Transition<? super Drawable> transition) {
                imageView.setImageDrawable(resource);
            }
        });
    }

    public static RequestOptions getRequestOption(int defaultImageId) {
        RequestOptions options = new RequestOptions()
                .placeholder(defaultImageId)
                .error(defaultImageId)
                .disallowHardwareConfig()
                .dontAnimate()
                .format(DecodeFormat.PREFER_RGB_565).diskCacheStrategy(DiskCacheStrategy.ALL);
        return options;
    }
    public static RequestOptions getRequestOption() {
        RequestOptions options = new RequestOptions()
                .placeholder(R.drawable.defalut_img)
                .error(R.drawable.defalut_img)
                .disallowHardwareConfig()
                .dontAnimate()
                .format(DecodeFormat.PREFER_RGB_565).diskCacheStrategy(DiskCacheStrategy.ALL);
        return options;
    }

    public static RequestOptions getRoundRequestOption(int radius, int defaultImageId) {
        RequestOptions options = new RequestOptions()
                .placeholder(defaultImageId)
                .error(defaultImageId)
                .transform(new GlideRoundTransform())
                .disallowHardwareConfig()
                .dontAnimate()
                .format(DecodeFormat.PREFER_RGB_565).diskCacheStrategy(DiskCacheStrategy.ALL);
        return options;
    }

    public static RequestOptions getRoundRequestOption(int radius, int corner, int defaultImageId) {
        RequestOptions options = new RequestOptions()
                .placeholder(defaultImageId)
                .error(defaultImageId)
                .transform(new GlideRoundTransform(corner, radius))
                .disallowHardwareConfig()
                .dontAnimate()
                .format(DecodeFormat.PREFER_RGB_565).diskCacheStrategy(DiskCacheStrategy.ALL);
        return options;
    }

    public static RequestOptions getCircleRequestOption(int defaultImageId) {
        RequestOptions options = new RequestOptions()
                .placeholder(defaultImageId)
                .error(defaultImageId)
                .circleCrop()
                .dontAnimate()
                .disallowHardwareConfig()
                .format(DecodeFormat.PREFER_RGB_565).diskCacheStrategy(DiskCacheStrategy.ALL);
        return options;
    }
}
